package enemies;

import game.Constants;
import game.Player;
import game.Explosion;

import Missiles.BossMissile;
import Missiles.EnemyBasicMissile;
import Missiles.Missile;

import java.util.*;
import java.awt.event.ActionEvent;

public class Boss extends StrongEnemy implements Constants{
	private ArrayList<BasicEnemy> enemies;
	private ArrayList<Explosion> expl;
	private Player player;
	private boolean dying;
	private int explNum;
	
	public Boss(ArrayList<Missile> m, ArrayList<BasicEnemy> e, Player target, ArrayList<Explosion> explosions)
	{
		super(MAIN_WIDTH / 2 - 75, m);
		damageHitPlayer = 13;  //player dies when colliding with the boss
		maxHP = 100;
		hp = maxHP;
		dropRate = 0;
		enemyMissiles = m;
		enemies = e;
		player = target;
		expl = explosions;
        setImage(BOSS);
        dying = false; 
        explNum = 0;
	}
	
	public void act()
	{
		x += dx; y += dy;
		if (x <= 0) {	x = 0; dx = -dx; }
		else if (x >= MAIN_WIDTH - width)
			{	x = MAIN_WIDTH - width; dx = -dx; }
		if (y <= 0) {	y = 0; dy = -dy; }
		else if (y >= 200)  //its y-coordinate is always less than 200
			{	y = 200; dy = -dy; }
		
		if (Math.random() < 0.04) 
			changeDir(); 
	}
	
	public void changeDir() //randomly change direction and speed
	{
		dx = Math.random()*4;
		dy = Math.random()*2;
	}
	
	public void actionPerformed(ActionEvent e)  //it fires all kinds of things
	{	
		if (Math.random() < 0.7)
			fireMissiles();
		else fireEnemies();
		
		//explode 4 times when dying
		if (dying){
			for (int j = 0; j < 4; j++)
			{
				int x = getX() + (int)(Math.random()*width);
				int y = getY() + (int)(Math.random()*height);
				expl.add(new Explosion(x,y));
			}
			explNum++;
			if (explNum == 4)
			{	
				expl.clear();
				dying = false; 
				timer.stop();
				die();	
			}
		}
	}
	
	public void fireMissiles() //2 types of missiles
	{
		if (Math.random() < 0.5)
		{
			for (double d = -Math.PI/6; d <= Math.PI/6; d += 0.1)
			{	enemyMissiles.add(new EnemyBasicMissile(getX() + width/2, getY() + height, d, false));	}
		}
		else {
			double angle = Math.atan(((player.getX() - (x + width/2))/
					(player.getY() - (getY() + height))));
			enemyMissiles.add(new BossMissile(getX() + width/2, getY() + height, angle, false));
		}
	}
	
	public void fireEnemies()
	{
		enemies.add(new SuperEnemy(getX() + width / 2, getY() + height, enemyMissiles));
	}
	
	public void beingHit(int damage)
	{
		hp -= damage;
		if (hp <= 0)
		{
			dying = true;
			timer.setDelay(500);
		}
	}
}
